//
//  ResetPwdViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/4.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "ResetPwdViewController.h"
#import "UnLineTextField.h"
#import "UserServer.h"

@interface ResetPwdViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UnLineTextField *pwdTF;
@property (nonatomic,strong) UnLineTextField *pwd2TF;
@property (nonatomic,strong) XThemeFlatButton *sureBtn;

@end

@implementation ResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)sureBtnClick{
    if (!self.pwdTF.text.length) {
        [self.view makeToast:self.pwdTF.placeholder];
        return;
    }
    if (![RegularExp isValidatePassword:self.pwdTF.text]) {
        [self.view makeToast:XLocalized(@"请设置有效密码!")];
        return;
    }
    if (!self.pwd2TF.text.length) {
        [self.view makeToast:self.pwd2TF.placeholder];
        return;
    }
    
    if (![self.pwdTF.text isEqualToString:self.pwd2TF.text]) {
        [self.view makeToast:XLocalized(@"密码不一致!")];
        return;
    }
    self.userServer.password = self.pwdTF.text;
    self.userServer.re_password = self.pwd2TF.text;
    kWeakSelf(self);
    [XAlertTool alertTitle:@"确定提交?" message:nil cancelAction:^{
    } sureAction:^{
        [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
        [weakself.userServer resetAccountPwdComplete:^(BOOL success, NSString * msg) {
            [MBProgressHUD hideHUDForView:weakself.view];
            if (success) {
                [XAlertTool alertTitle:msg message:@"是否立即登录?" cancelAction:^{
                    
                } sureAction:^{
                    RootNavigationController*rootNav = (RootNavigationController*)weakself.navigationController;
                    [rootNav popToAppointViewController:@"LoginViewController" animated:YES];
                }];
            }else{
                [weakself.view makeToast:msg];
            }
        }];
    }];
}

- (void)setupUI{
    self.title = XLocalized(@"设置密码");

    [self.contentView addSubview:self.pwdTF];
    [self.pwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.top.mas_equalTo(AdaptedHeight(40));
    }];
    
    [self.contentView addSubview:self.pwd2TF];
    [self.pwd2TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.top.equalTo(self.pwdTF.mas_bottom).offset(AdaptedHeight(30));
    }];
    
    [self.contentView addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.top.equalTo(self.pwd2TF.mas_bottom).offset(AdaptedHeight(95));
        make.bottom.mas_equalTo(AdaptedHeight(-150));
    }];
    
    UILabel*tipLab = [UILabel new];
    tipLab.numberOfLines = 0;
    tipLab.font = kDefaultFont(10);
    tipLab.textColor = UIColorHex(999999);
    tipLab.text =XLocalized(@"密码长度8-16位，须包含数字、字母、符号至少2种或以上元素。");
    [self.contentView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.top.equalTo(self.pwd2TF.mas_bottom).offset(AdaptedHeight(10));
    }];
    kWeakSelf(self);
    [self.sureBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself sureBtnClick];
    }];
}

- (UnLineTextField*)pwdTF{
    if (!_pwdTF) {
        _pwdTF = [UnLineTextField new];
        _pwdTF.delegate = self;
        _pwdTF.placeholder = XLocalized(@"请输入密码");
        _pwdTF.font = kDefaultFont(15);
        _pwdTF.textColor = UIColorHex(333333);
        [_pwdTF setValue:@16 forKey:Limit_NAME];
        UIButton *eyeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        kWeakSelf(eyeBtn);
        kWeakSelf(self);
        [eyeBtn addTapBlock:^(UIButton * _Nonnull btn) {
            weakeyeBtn.selected = !weakeyeBtn.selected;
            if (weakeyeBtn.selected) {
                weakself.pwdTF.secureTextEntry = NO;
            }else{
                weakself.pwdTF.secureTextEntry = YES;
            }
        }];
        _pwdTF.secureTextEntry = YES;
        [eyeBtn setImage:[UIImage imageNamed:@"close_eye"] forState:UIControlStateNormal];
        [eyeBtn setImage:[UIImage imageNamed:@"open_eye"] forState:UIControlStateSelected];
        _pwdTF.rightViewMode = UITextFieldViewModeAlways;
        _pwdTF.rightView = eyeBtn;
        _pwdTF.keyboardType = UIKeyboardTypeASCIICapable;
        _pwdTF.returnKeyType = UIReturnKeyDone;
    }
    return _pwdTF;
}

- (UnLineTextField*)pwd2TF{
    if (!_pwd2TF) {
        _pwd2TF = [UnLineTextField new];
        _pwd2TF.delegate = self;
        _pwd2TF.placeholder = XLocalized(@"请再次输入密码");
        _pwd2TF.font = kDefaultFont(15);
        _pwd2TF.textColor = UIColorHex(333333);
        UIButton *eyeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        kWeakSelf(eyeBtn);
        kWeakSelf(self);
        [eyeBtn addTapBlock:^(UIButton * _Nonnull btn) {
            weakeyeBtn.selected = !weakeyeBtn.selected;
            if (weakeyeBtn.selected) {
                weakself.pwd2TF.secureTextEntry = NO;
            }else{
                weakself.pwd2TF.secureTextEntry = YES;
            }
        }];
        _pwd2TF.secureTextEntry = YES;
        [eyeBtn setImage:[UIImage imageNamed:@"close_eye"] forState:UIControlStateNormal];
        [eyeBtn setImage:[UIImage imageNamed:@"open_eye"] forState:UIControlStateSelected];
        _pwd2TF.rightViewMode = UITextFieldViewModeAlways;
        _pwd2TF.rightView = eyeBtn;
        _pwd2TF.keyboardType = UIKeyboardTypeASCIICapable;
        _pwd2TF.returnKeyType = UIReturnKeyDone;
        [_pwd2TF setValue:@16 forKey:Limit_NAME];
    }
    return _pwd2TF;
}

- (XThemeFlatButton*)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [XThemeFlatButton button];
        [_sureBtn setTitle:XLocalized(@"提交") forState:UIControlStateNormal];
    }
    return _sureBtn;
}

- (UserServer*)userServer{
    if (!_userServer) {
        _userServer = [UserServer new];
    }
    return _userServer;
}

@end
